<?php 
session_start();
class deal extends CI_Controller  {
        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->load->library('encrypt');
            $this->_configs["title"]="Quản Lý Deal";
            $this->_configs["authority"]="deal";
            $this->_configs["MinAuthority"]=0;
            $this->load->model('deal/category_model','category_model');
            $this->load->model('deal/product_model','product_model');
            $this->load->model('deal/customer_model','customer_model');
            $this->load->model('admin-planners/region_model','region_model');
            $_SESSION["DRegion"]=isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
            $_SESSION["DRegionName"]=isset($_SESSION["DRegionName"])?$_SESSION["DRegionName"]:"Toàn Quốc";
        }
        public function index()
	{
            redirect(base_url("deal/recent"));
           
	}
        function swithlocation($location=""){
            $_SESSION["DRegion"]=$location;
            if($_SESSION["DRegion"]==""){
                $_SESSION["DRegionName"]="Toàn Quốc";
            }else{
                $region= $this->region_model->getById($location);
                $_SESSION["DRegionName"]=$region[0]->name;
            }
            echo "<script>window.location='http://dealgiadung.com/san-pham';</script>";
            redirect(base_url("san-pham"));
        }
        public function today($Category=""){
            $Configs=null;
            $Configs["Category"]=$Category;
            $Configs["dealtype"]="san-pham-hom-nay";
            $Configs["menu"]="today";
            if($Category=="uu-dai-hot")$Configs["menu"]="hot";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $num=8;
            $begin=0;
            $data["categorys"]  =   objectToArray($this->category_model->gets());
            $data["products"]   =   objectToArray($this->product_model->gets($begin,$num,$Category,true));
            for ($i=0;$i<count($data["products"]);$i++){
                $data["products"][$i]["Discount"]=round(($data["products"][$i]["UnitPrice"]-$data["products"][$i]["UnitsOnOrder"])*100/$data["products"][$i]["UnitPrice"],0);
            }
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"         );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"      );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"        );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"        );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"        );
            $this->smarty->view     (   "deal/pages/01_home"     ,"dpage"        ); 
            $this->smarty->display  (   "deal/01_home"                          );
	}
        public function recent($Category="",$page=0){
            if($Category=="tat-ca")$Category="";
            $Configs=null;
            $Configs["Category"]=$Category;
            $Configs["dealtype"]="san-pham";
            $Configs["menu"]="recent";
            if($Category=="uu-dai-hot")$Configs["menu"]="hot";
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"])){$Configs["U"]=$_SESSION["D-USER"];}
            $num=62;
            $begin=$page*62;
            $data["begin"]=$begin;
            $data["categorys"]  =   objectToArray($this->category_model->gets());
            $data["products"]   =   $this->product_model->gets($begin,$num,$Category);
            $data["count"]      =   $this->product_model->counts($Category);
            $data["count"]=$data["count"][0]->num;
            //if($data["count"]%62!=0)
            $data["pages"]=intval($data["count"]/62);
            $data["page"]=$page;
            $data["products"]=objectToArray($data["products"]);
            for ($i=0;$i<count($data["products"]);$i++){
                $data["products"][$i]["Discount"]=round(($data["products"][$i]["UnitPrice"]-$data["products"][$i]["UnitsOnOrder"])*100/$data["products"][$i]["UnitPrice"],0);
            }
            $data["regions"]=  objectToArray($this->region_model->getByParent("0"));
            $Configs["Foot"]=@file_get_contents(APPPATH."HTML_DOM/Foot.txt");
            $this->smarty->assign('data', $data);
            $this->smarty->assign('Configs', $Configs);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->view     (   "deal/subs/00_css"       ,"dcss"        );
            $this->smarty->view     (   "deal/subs/00_script"    ,"dscript"     );
            $this->smarty->view     (   "deal/subs/01_head"      ,"dhead"       );
            $this->smarty->view     (   "deal/subs/02_menu"      ,"dmenu"       );
            $this->smarty->view     (   "deal/subs/03_foot"      ,"dfoot"       );
            $this->smarty->view     (   "deal/pages/01_home"     ,"dpage"       ); 
            $this->smarty->display  (   "deal/01_home"                          );
	}
        function otherProduct($begin=0,$Category="",$today=""){
            if($Category=="tat-ca")$Category="";
            if($today=="")$today=false;
            else $today=true;
            $num=9;
            $Configs["Category"]=$Category;
            $data["products"]   =   objectToArray($this->product_model->gets($begin,$num,$Category,$today));
            if(count($data["products"])>0){
                for ($i=0;$i<count($data["products"]);$i++){
                    $data["products"][$i]["Discount"]=round(($data["products"][$i]["UnitPrice"]-$data["products"][$i]["UnitsOnOrder"])*100/$data["products"][$i]["UnitPrice"],0);
                }
                $this->smarty->assign('data', $data);
                $this->smarty->assign('Configs', $Configs);
                $this->smarty->display  (   "deal/pages/02_other_product"                          );
            }
        }
        public function logout()
	{
            session_destroy();
            echo "<script>window.location='http://dealgiadung.com';</script>";
            redirect("http://dealgiadung.com");
	}
        function register(){
            $code=-1;
            $msg="Đăng ký thất bại";
            //$this->load->library('KUMail/KUMail','','KUMail');
            //$checkemail=$this->KUMail->CheckEmailValidate($_POST["Mail"]);
            $account=$this->customer_model->get($_POST["Mail"]);
            if(count($account)==0){
                if(preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["Mail"]) === 0){
                    $code=-1;
                    $msg="Email không hợp lệ";
                }else{
                    $Cus=$_POST;
                    $Cus["CustomerID"]="D".strtoupper(md5($Cus["Mail"]));
                    if($this->customer_model->insert($Cus)){
                        $code=1;
                        $msg="Đăng ký thành công.";
                        $account=$this->customer_model->get($_POST["Mail"]);
                        $_SESSION["D-USER"]=objectToArray($account[0]);
                        $params_mail=array("Email"=>$Cus["Mail"]);
                        $this->customer_model->insertMail($params_mail);
                    }else{
                        $code=-1;
                        $msg="Đăng ký thất bại.";
                    }
                }
            }else{
                $code=-1;
                $msg="E-mail đã được đăng ký!.";
            }
            $rs=array("code"=>$code,"msg"=>$msg);
            echo json_encode($rs);
        }
        function loadaccountinfo(){
            echo"
                <div class=\"duser dpr\">
                    <image src=\"".$_SESSION["D-USER"]["Avatar"]."\"/>
                </div>
                <div style=\"float: left;padding-right: 12px\">
                    <a href=\"".base_url('customer/information')."\" style=\"padding: 0 12px\">".$_SESSION["D-USER"]["CustomerName"]."</a>
                    | 
                    <a href=\"".base_url('deal/logout')."\" style=\"padding: 0 12px\">Thoát</a>
                </div>
            ";
        }
        function login(){
            if(!(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["Mail"])>0)){
                $result=array(
                "code"      =>  -1,
                "msg"       => "Đăng nhập thất bại. Email không hợp lệ."
                );
                echo json_encode($result);
                return;
            }
            $account=$this->customer_model->get($_POST["Mail"]);
            if(count($account)>0){
                if($account[0]->Type=="Google" || $account[0]->Type=="Yahoo" || $account[0]->Type=="FaceBook"){
                    $result=array(
                    "code"      =>  -1,
                    "msg"       => "E-mail này chỉ có thể đăng nhập từ OpenID(".$account[0]->Type.")"
                    );
                }elseif($_POST["Password"]==$account[0]->Password){
                    $_SESSION["D-USER"]=objectToArray($account[0]);
                    $result=array(
                    "code"      =>  1,
                    "msg"       => "Đăng nhập thành công."
                    );
                }else{
                    $result=array(
                    "code"      =>  -1,
                    "msg"       => "Mật khẩu chưa chính xác."
                    );
                }
            }else{
                $result=array(
                "code"      =>  -1,
                "msg"       => "Đăng nhập thất bại. Tài khoản không tồn tại."
                );
            }
            echo json_encode($result);
        }        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */